const readline = require('readline');

const input = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

interface DialogDirector {
    showDialog(): void
    widgetChanged(w: Widget): void
    createWidgets(): void
}
type WidgetSetObj = {
    "ok": Button,
    "cancel": Button,
    "fontList": ListBox,
    "fontName": EntryField
}
class FontDialogDirector implements DialogDirector {
    declare private _ok: Button
    declare private _cancel: Button
    declare private _fontList: ListBox
    declare private _fontName: EntryField
    showDialog(): void {
        console.log(`
        ┌──────────────────────┐
        │　　更改字体对话框　　│
        │　　　-${this._fontName.getText()}-　　　　　│ 
        │　　选择字体区域　　　│
        │　　　${this._fontList.getList().get(0)}[０]　　　　│
        │　　　${this._fontList.getList().get(1)}[１]　　　　│
        │　　　${this._fontList.getList().get(2)}[２]　　　　│
        │　　　　　　　　　　　│
        │　确定[1]　　取消[0]　│
        └──────────────────────┘
        `);
    }
    widgetChanged(w: Widget): void {
        if (w === this._fontList) {
            this._fontName.setText(this._fontList.getSelection())
        } else if (w === this._ok) {
            //应用字体改变
            console.log('应用字体');

        } else if (w === this._cancel) {
            //取消
            console.log("取消");
        }
    }
    createWidgets(): void {
        this._ok = new Button(this)
        this._ok.setText("确定")

        this._cancel = new Button(this)
        this._cancel.setText("取消")

        this._fontList = new ListBox(this)
        const list = new List<string>()
        list.set("宋体")
        list.set("黑体")
        list.set("仿宋")
        this._fontList.setList(list)

        this._fontName = new EntryField(this)
    }

    getWidget(): WidgetSetObj {
        return {
            "ok": this._ok,
            "cancel": this._cancel,
            "fontList": this._fontList,
            "fontName": this._fontName
        }
    }
}
export class Widget {
    declare private _dialogDirector: DialogDirector
    constructor(dialogD: DialogDirector) {
        this._dialogDirector = dialogD
    }
    changed(): void {
        this._dialogDirector.widgetChanged(this)
    }
    handleMouse(event: MouseEvent) { }
}
class ListBox extends Widget {

    declare private _listItem: List<string>
    declare private _selected: string
    constructor(dialogD: DialogDirector) {
        super(dialogD)
    }
    getSelection(): string { return this._selected }
    setList(listItem: List<string>): void {
        this._listItem = listItem
    }
    getList(): List<string> {
        return this._listItem
    }
    updateSelected(index: number) {
        this._selected = this._listItem.get(index)
    }
    handleMouse(event: MouseEvent): void {

    }
}
class EntryField extends Widget {

    declare private _text: string
    constructor(dialogD: DialogDirector) {
        super(dialogD)
        this._text = "　　"
    }
    getText(): string { return this._text }
    setText(str: string): void {
        this._text = str
    }
    handleMouse(event: MouseEvent): void {

    }
}
class Button extends Widget {

    constructor(dialogD: DialogDirector) {
        super(dialogD)
    }
    setText(str: string): void { }
    handleMouse(event: MouseEvent): void {
        this.changed()
    }
}
const DEFAULT_LIST_CAPACITY = 10
class List<Item> {
    declare private _size: number
    declare private _arr: Item[]
    constructor(size: number = DEFAULT_LIST_CAPACITY) {
        this._size = size
        this._arr = []
    }
    count(): number {
        return this._arr.length
    }
    get(index: number): Item {
        return this._arr[index]
    }
    set(i: Item): void {
        this._arr.push(i)
    }
}

const fontDialog = new FontDialogDirector()
fontDialog.createWidgets()
fontDialog.showDialog()

input.question('请选择字体：', (in1: string) => {
    const i = parseInt(in1, 10);
    fontDialog.getWidget().fontList.updateSelected(i)
    fontDialog.widgetChanged(fontDialog.getWidget().fontList)

    fontDialog.showDialog()
    input.question('确定还是取消？', (in2: string) => {
        const number = parseInt(in2, 10);
        switch (number) {
            case 0:
                fontDialog.widgetChanged(fontDialog.getWidget().cancel)
                input.close()
                break

            case 1:
                fontDialog.widgetChanged(fontDialog.getWidget().ok)
                input.close()
                break
            default:
                console.log('输入异常');
                input.close()
        }
    })
})





